package com.leapp.sk.viewdelegate

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.*
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.example.BaseNewApplication
import com.example.mvp.adapter.NewBannerAdpter
import com.example.mvp.bean.BannerDataEntity
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.*
import com.example.util.DetailImgurlsUtilMove
import com.example.util.ISHAVECLASS
import com.example.util.ISTECHER
import com.leapp.sk.R
import com.leapp.sk.app.SKApplication
import com.leapp.sk.bean.TextImageEntity
import com.leapp.sk.ui.activity.AppMainTableActivity
import com.leapp.sk.ui.activity.album.AlbumManagerActivity
import com.leapp.sk.ui.activity.archives.BabyArchivesActivity
import com.leapp.sk.ui.activity.classs.ClassAlbumActivity
import com.leapp.sk.ui.activity.join.JoinClassExaminationActivity
import com.leapp.sk.ui.activity.leave.LevelActivity
import com.leapp.sk.ui.activity.leave.TeacherLeaveActivity
import com.leapp.sk.ui.activity.notify.ClassNotifyActivity
import com.leapp.sk.ui.activity.task.ParentingTaskActivity
import com.leapp.sk.ui.activity.task.TeacherParentTaskActivity
import com.leapp.sk.ui.adapter.CommonFunctionAdapter
import com.example.util.finallist.SkKey

/**
 * 应用主界面视图托管
 * @author  chaohaipeng  on 2018/7/25.
 */
class ApplicationDelegate : BaseAppDelegate() {
    /**
     * 轮播工具类
     */
    private var detailImgurlsUtilMove: DetailImgurlsUtilMove? = null

    /**
     * 拦截dialog
     */
    private var lkDialog: LKDialog? = null

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.fragment_application
    }

    /**
     * 设置标题
     */
    fun setTitle(title: String?) {
        val topBar = getView<SKTopBar>(R.id.jtb_fa)
        topBar.rl_ljtb_left_layout?.visibility = View.GONE
        if (!TextUtils.isEmpty(title)) {
            topBar?.setTitle(title)
        }
    }

    /**
     * 初始化常用功能适配器
     */
    fun initFunction(mlist: List<TextImageEntity>) {
        val gv_common_function = getView<NoScrollGridView>(R.id.gv_common_function)
        val commonFunctionAdapter = CommonFunctionAdapter(getActivity(), mlist)
        gv_common_function.adapter = commonFunctionAdapter
        initEvent()
    }

    /**
     * 初始化事件
     */
   private fun initEvent(){
        val gv_common_function = getView<NoScrollGridView>(R.id.gv_common_function)
        gv_common_function.setOnItemClickListener{adapterView: AdapterView<*>?, view: View?, i: Int, l: Long ->
            run {
                initIntent(i)

            }
        }
    }

    /**
     * 初始化跳转
     */
    private fun initIntent(position:Int){
        when(position){
            SkKey.BTKEY_INT_0->{
                val noJoinClass = noJoinClass()
                if (!noJoinClass){
                    val intent=Intent(getActivity(), ClassNotifyActivity::class.java)
                    getActivity<AppMainTableActivity>().startActivity(intent)
                }
            }
            SkKey.BTKEY_INT_1->{
                val noJoinClass = noJoinClass()
                if (!noJoinClass){
                    if (ISTECHER){
                        val intent=Intent(getActivity(), TeacherParentTaskActivity::class.java)
                        getActivity<AppMainTableActivity>().startActivity(intent)
                    }else{
                        val intent=Intent(getActivity(), ParentingTaskActivity::class.java)
                        getActivity<AppMainTableActivity>().startActivity(intent)

                    }
                }

            }
            SkKey.BTKEY_INT_2->{
                val noJoinClass = noJoinClass()
                if (!noJoinClass){
                    if (ISTECHER){
                        val intent=Intent(getActivity(), AlbumManagerActivity::class.java)
                        getActivity<AppMainTableActivity>().startActivity(intent)
                    }else{
                        val intent=Intent(getActivity(), ClassAlbumActivity::class.java)
                        getActivity<AppMainTableActivity>().startActivity(intent)
                    }
                }

            }
            SkKey.BTKEY_INT_3->{
                if (ISTECHER){

                        val intent=Intent(getActivity(), JoinClassExaminationActivity::class.java)
                        getActivity<AppMainTableActivity>().startActivity(intent)

                }else{
                    val intent=Intent(getActivity(), BabyArchivesActivity::class.java)
                    getActivity<AppMainTableActivity>().startActivity(intent)

                }
            }
            SkKey.BTKEY_INT_4->{
                if (ISTECHER){
                    val intent=Intent(getActivity(), TeacherLeaveActivity::class.java)
                    getActivity<AppMainTableActivity>().startActivity(intent)
                }else{
                    val noJoinClass = noJoinClass()
                    if (!noJoinClass){
                        val intent=Intent(getActivity(), LevelActivity::class.java)
                        getActivity<AppMainTableActivity>().startActivity(intent)
                    }

                }

            }
        }


    }

    /**
     * 未加入班级弹出拦截
     */
    fun noJoinClass():Boolean{
        if (!ISHAVECLASS&&!ISTECHER){
            if (lkDialog==null){
                lkDialog = LKDialog(getActivity())
                lkDialog?.setMessage(com.example.baselibrary.R.string.str_no_join_class)
                lkDialog?.setRightButton(com.example.baselibrary.R.string.string_confirm, View.OnClickListener {
                            lkDialog?.dismiss()
                    BaseNewApplication.getInstance().loginOut(SKApplication.getInstance())
                        })
            }
            lkDialog?.show()
            return true
        }else{
           return false
        }
}

    /**
     * 初始化Banner数据
     */
    fun initBanner(data: ArrayList<BannerDataEntity>) {
        val ll_fa_point = getView<LinearLayout>(R.id.ll_fa_point)
        val vp_fa = getView<CustomViewPager>(R.id.vp_fa)

        val rl_fa_banner = getView<RelativeLayout>(R.id.rl_fa_banner)
        val layoutParams: LinearLayout.LayoutParams = rl_fa_banner.layoutParams as LinearLayout.LayoutParams
        layoutParams.height = LKScreenUtil.getScreenWidth() / 2
        rl_fa_banner.layoutParams = layoutParams
        val newBannerAdpter = NewBannerAdpter(getActivity(), data)
        vp_fa.adapter = newBannerAdpter
        detailImgurlsUtilMove = DetailImgurlsUtilMove(ll_fa_point, getActivity(), SKApplication.getInstance()?.mHandler, vp_fa)
        detailImgurlsUtilMove!!.addViewpgPoint(data)
    }
    /**
     * 停止运行
     */
    fun stopRuning() {
            detailImgurlsUtilMove?.destoryRunning()


    }


}